package com.desaweb.pe.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.desaweb.pe.data.DatabaseManager;
import com.desaweb.pe.form.ServicioForm;

/**
 * Servlet implementation class ServicioServlet
 */
public class ServicioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String ACTIVE_MENU="menu_service";
	private static final String FORM_PAGE="/pages/servicio/formulario.jsp";
	private static final String LIST_PAGE="/pages/servicio/consulta.jsp";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServicioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setAttribute("active_menu", ACTIVE_MENU);
		String template = request.getParameter("template");
	
		RequestDispatcher view = null ;
		if(template.equalsIgnoreCase("consulta")){
			request.setAttribute("servicios", DatabaseManager.getInstance().obtenerServicios());
			view = request.getRequestDispatcher(LIST_PAGE);
			
		}else if(template.equalsIgnoreCase("registro")){
			initForm(request);
			
			view = request.getRequestDispatcher(FORM_PAGE);
		}
		view.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
	
		
		request.setAttribute("active_menu", ACTIVE_MENU);
		String template = request.getParameter("template");
		String action = request.getParameter("action");
		RequestDispatcher view = null ;
		if(template.equalsIgnoreCase("consulta")){
			
		}else if(template.equalsIgnoreCase("registro")){
			
			
			ServicioForm servicioForm = new ServicioForm();			
			servicioForm.setNombre(request.getParameter("_nombre"));
			servicioForm.setDescripcion(request.getParameter("_descripcion"));
			servicioForm.setCosto(request.getParameter("_costo"));
			servicioForm.parseForm();
				if (servicioForm.isValidForm()){
					request.setAttribute("isSaved", Boolean.TRUE);
					DatabaseManager.getInstance().grabarServicio(servicioForm.getObjeto());
					initForm(request);
				}else{
					
					request.setAttribute("hasErrors", Boolean.FALSE);
					request.setAttribute("errors", servicioForm.getErrors().values().toArray());										
						
				}
				view = request.getRequestDispatcher(FORM_PAGE);
				
		}
		view.forward(request, response);
		
		
	}
	
	private void initForm(HttpServletRequest request){
		String paramId=request.getParameter("param");
		ServicioForm servicioForm = new ServicioForm();
		if(paramId !=null){
		servicioForm.populateForm(DatabaseManager.getInstance().obtenerServicio(Integer.parseInt(paramId)));
		}
		request.setAttribute("servicioForm", servicioForm);
	}
	

}
